package com.lin.linchatgpt.type_handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Copyright(C),2022年-2022,霖霖
 *
 * @author 霖霖
 * @version 1.0
 * @date 2023/7/18 6:55
 * @Description String[]映射
 */
@MappedTypes(String[].class)
public class StringArrayHandler extends BaseTypeHandler<String[]> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
        String arrSting = String.join(",", parameter);
        ps.setString(i, arrSting);
    }

    @Override
    public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String arrString = rs.getString(columnName);
        if (arrString != null)
            return arrString.split(",");
        return null;
    }

    @Override
    public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String arrString = rs.getString(columnIndex);
        if (arrString != null)
            return arrString.split(",");
        return null;
    }

    @Override
    public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String arrString = cs.getString(columnIndex);
        if (arrString != null)
            return arrString.split(",");
        return null;
    }
}
